package ideamaps.settings;

import java.io.Serializable;
import java.util.List;


import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
//@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "custlist")
public class Custlist  implements Serializable{
	
	private static final long serialVersionUID = -3065333913742343749L;

	private Long id;
	
	private String name;
	private String abbr;
	
	private List<CustlistItem> items;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public String getAbbr() {
		return abbr;
	}
	public void setAbbr(String abbr) {
		this.abbr = abbr;
	}
	

	/*
	@JoinTable(
			name="youkaka_Custlist_join_item",
			joinColumns={@JoinColumn(name="Custlist_id")},
			inverseJoinColumns={@JoinColumn(name="item_id")}
	)
	*/

	@OneToMany(fetch=FetchType.LAZY,cascade={CascadeType.ALL})
	@JoinColumn(name="custlist_id")
	@OrderBy(value = "sequence ASC") 
	//@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	public List<CustlistItem> getItems() {
		return items;
	}
	public void setItems(List<CustlistItem> items) {
		this.items = items;
	}
	
	
	
	
	@Transient
	public int getUniqueIdByAbbr(String abbr){
		if(abbr==null){
			return 0;
		}
		
		CustlistItem item = getItemByAbbr(abbr);
		if(item==null){
			return 0;
		}else{
			return item.getUniqueId();
		}
	}
	
	@Transient
	public String getAbbrByUniqueId(int uniqueId){
		CustlistItem item = getItemByUniqueId(uniqueId);
		if(item==null){
			return "";
		}else{
			return item.getAbbr();
		}
	}
	
	
	@Transient
	public String getValueByAbbr(String abbr){
		if(abbr==null)	return "";
		CustlistItem item = getItemByAbbr(abbr);
		return item==null? "": item.getValue();
	}
	
	
	@Transient
	public String getValue2ByAbbr(String abbr){
		if(abbr==null)	return "";
		CustlistItem item = getItemByAbbr(abbr);
		return item==null? "": item.getValue2();
	}
	
	@Transient
	public String getValueByUniqueId(int uniqueId){
		CustlistItem item = getItemByUniqueId(uniqueId);
		return item==null? "": item.getValue();
	}
	
	@Transient
	public String getValue2ByUniqueId(int uniqueId){
		CustlistItem item = getItemByUniqueId(uniqueId);
		return item==null? "": item.getValue2();
	}
	
	
	@Transient
	public CustlistItem getItemByUniqueId(int uniqueId){
		if(items!=null){
			for(CustlistItem item : items){
				if(item.getUniqueId() == uniqueId){
					return item;
				}
			}
		}
		return null;
	}
	
	
	@Transient
	public CustlistItem getItemByAbbr(String abbr){
		if(items!=null){
			for(CustlistItem item : items){
				if(abbr.equals(item.getAbbr())){
					return item;
				}
			}
		}
		return null;
	}
	
	
}
